# Contributor: Timo Teräs <timo.teras@iki.fi>
# Maintainer: Timo Teräs <timo.teras@iki.fi>
pkgname=fwupd
pkgver=1.8.2
pkgrel=0
pkgdesc="Firmware update daemon"
url="https://fwupd.org/"
# armhf, ppc64le, s390x and riscv64 blocked by polkit and gnu-efi
arch="all !armhf !ppc64le !s390x !riscv64"
license="LGPL-2.1-or-later"
depends="dbus"
makedepends="meson help2man linux-headers gnutls-utils tpm2-tss-esys
	python3 py3-pillow py3-gobject3 py3-cairo ttf-opensans
	glib-dev libgudev-dev libgusb-dev libgcab-dev json-glib-dev
	gobject-introspection-dev vala cairo-dev fontconfig-dev freetype-dev
	polkit-dev libxmlb-dev libarchive-dev libsoup-dev gpgme-dev eudev-dev
	gnutls-dev efivar-dev elfutils-dev gnu-efi-dev tpm2-tss-dev elogind-dev
	libjcat-dev py3-setuptools fwupd-efi-dev py3-markdown
	modemmanager-dev libqmi-dev libmbim-dev protobuf-c-dev
	"
checkdepends="bubblewrap"
subpackages="
	$pkgname-dev
	$pkgname-doc
	$pkgname-lang
	$pkgname-openrc
	$pkgname-fish-completion
	"
source="https://github.com/hughsie/fwupd/archive/$pkgver/fwupd-$pkgver.tar.gz
	$pkgname.initd
	"

_plugins="
	acpi_facp
	acpi_phat
	amt
	analogix
	ata
	bcm57xx
	bios
	ccgx
	cfu
	ch341a
	colorhug
	corsair
	cpu
	cros_ec
	dell_dock
	dfu
	dfu_csr
	ebitdo
	elanfp
	elantp
	emmc
	ep963x
	fastboot
	fresco_pd
	genesys
	goodixmoc
	gpio
	hailuck
	iommu
	jabra
	lenovo_thinklmi
	linux_lockdown
	linux_sleep
	linux_swap
	linux_tainted
	logind
	logitech_bulkcontroller
	logitech_hidpp
	modem_manager
	mtd
	nitrokey
	nordic_hid
	nvme
	optionrom
	parade_lspcon
	pci_bcr
	pci_mei
	pixart_rf
	powerd
	realtek_mst
	rts54hid
	rts54hub
	scsi
	steelseries
	superio
	synaptics_cape
	synaptics_cxaudio
	synaptics_mst
	synaptics_prometheus
	synaptics_rmi
	system76_launch
	thelio_io
	thunderbolt
	tpm
	uefi_dbx
	uefi_pk
	uefi_recovery
	uf2
	upower
	usi_dock
	vbe
	vli
	wacom_raw
	wacom_usb
	"

# libsmbios is a library for DELL devices which are x86/x86_64-only
case "$CARCH" in
	x86*)
		makedepends="$makedepends libsmbios-dev"
		_plugins="$_plugins acpi_dmar acpi_ivrs dell dell_esrt msr"
		;;
	*) 	;;
esac

case "$CARCH" in
	x86_64)
		_plugins="$_plugins pci_psp"
		;;
	*)	;;
esac

case "$CARCH" in
	x86*|armv7|aarch64)
		# fwupd-efi needed for uefi-capsule
		_plugins="$_plugins uefi_capsule"
		_uefi_capsule=enabled
		;;
	*)
		_uefi_capsule=disabled
		;;
esac

for _plugin in $_plugins; do
	subpackages="$subpackages $pkgname-plugin-$_plugin:_split"
done
subpackages="$subpackages $pkgname-plugin-all:plugin_all"

build() {
	abuild-meson \
		-Dconsolekit=enabled \
		-Dfirmware-packager=true \
		-Ddocs=none \
		-Dlvfs=true \
		-Dman=true \
		-Dplugin_redfish=disabled \
		-Dplugin_uefi_capsule=$_uefi_capsule \
		-Dsystemd=disabled \
		-Delogind=enabled \
		-Dtests=true \
		-Dudevdir=/lib/udev \
		. output

	# HACK: meson seems to not be running the custom generations in the correct
	# order, causing the build to fail because of the missing headers, the
	# second one generates .c and .h files
	ninja -C output libfwupdplugin/fu-hash.h
	ninja -C output src/fwupdtool.p/meson-generated_.._fwupd-resources.c.o

	meson compile ${JOBS:+-j ${JOBS}} -C output
}

check() {
	# tests fail if usb is not available
	[ -e /dev/bus/usb ] || return 0
	meson test --no-rebuild -v -C output
}

package() {
	DESTDIR="$pkgdir" meson install --no-rebuild -C output

	install -Dm755 "$srcdir"/$pkgname.initd "$pkgdir"/etc/init.d/$pkgname

	mkdir -p "$pkgdir"/usr/share/fish
	mv "$pkgdir"/usr/share/fish/vendor_completions.d "$pkgdir"/usr/share/fish/completions
}

_split() {
	local plugin="${subpkgname##*-}"
	pkgdesc="$pkgdesc ($plugin plugin)"

	case "$plugin" in
		uefi_capsule)
			depends="fwupd-efi"
			;;
		*)
			;;
	esac

	amove usr/lib/fwupd-plugins-7/libfu_plugin_$plugin.so
}

plugin_all() {
	pkgdesc="$pkgdesc (plugin meta package)"
	mkdir -p "$subpkgdir"

	for _plugin in $_plugins; do
		depends="$depends $pkgname-plugin-$_plugin"
	done
}

sha512sums="
195faa5223a10dc8e18bf54f6471dc96b46bbd827f754a0d31107b0b4a295ab5d4040ba8f655f7ccc897dc5a6510927252501c3bb091e3af6dbdba0885476bbd  fwupd-1.8.2.tar.gz
4add6954bb3581b371d6105addde819cd41834d33293fe7362dbab4e0dccf486e2bbc96d5822fcb729ca568739cc5572c8e30d3db220f4476befbdc85ba8457b  fwupd.initd
"
